﻿using System;
using System.Collections;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Xml.Linq;
using System.Text;
using System.Collections.Generic;

namespace Jy123.Web.Module.CarClub
{

    public class GetFlash : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strResult = context.Cache["readyforbattle"].ToString();
            string type = context.Request.QueryString["Type"];
            if (string.IsNullOrEmpty(type))
            {
                type = context.Request["Type"];
            }
            string url = context.Request.Url.ToString();
            string strClubId = context.Request.QueryString["ModuleId"];
            if (strClubId.Contains("GetStar"))
            {
                type = "GetStar";
            }
            else if (strClubId.Contains("GetCar"))
            {
                type = "GetCar";
            }
            if (strClubId.Contains('T'))
            {
                type = strClubId.Substring(strClubId.LastIndexOf('T') + 1);
                strClubId = strClubId.Substring(0, strClubId.LastIndexOf('T'));

            }
            else if (strClubId.Contains(','))
            {
                strClubId = strClubId.Split(',')[0];
            }
            StringBuilder sbString = new StringBuilder();
            if (!string.IsNullOrEmpty(strClubId))
            {
                int clubId = int.Parse(strClubId);
                BLL.Club_Main BLLClub = new Jy123.BLL.Club_Main();
                Model.Club_Main club = BLLClub.GetModel(clubId);
                BLL.User_Main BLLUser = new Jy123.BLL.User_Main();
                Model.User_Main user = BLLUser.GetModel(club.FUser.Value);
                string strXML;
                if (type.Equals("Star"))
                {
                    string html = context.Cache["clubwear"].ToString();
                    string strObject = context.Cache["flash"].ToString();
                    //strXML = string.Format("/Module/CarClub/GetFlash.ashx?ModuleId={0},GetStar&Guid={1}".Trim(), strClubId, Guid.NewGuid());
                    strXML = string.Format("/Module/CarClub/XML/{0}TGetStar/{1}.xml", strClubId, Guid.NewGuid());
                    strObject = strObject.Replace("$XML$", strXML);
                    //strObject = strObject.Replace("$XML$", "/Cache/Module/Flash/test.xml");
                    strObject = strObject.Replace("$Value$", "/Cache/Module/Flash/Court.swf");
                    html = html.Replace("$Object$", strObject);
                    context.Response.Write(html);
                }
                else if (type.Equals("Car"))
                {
                    string html = context.Cache["clubwear"].ToString();
                    string strObject = context.Cache["flash"].ToString();
                    //strXML = string.Format("/Module/CarClub/GetFlash.ashx?ModuleId={0},GetCar&Guid={1}".Trim(), strClubId, Guid.NewGuid());
                    strXML = string.Format("/Module/CarClub/XML/{0}TGetCar/{1}.xml", strClubId, Guid.NewGuid());
                    strObject = strObject.Replace("$XML$", strXML);
                    //strObject = strObject.Replace("$XML$", "/Cache/Module/Flash/test.xml");
                    strObject = strObject.Replace("$Value$", "/Cache/Module/Flash/Court.swf");
                    html = html.Replace("$Object$", strObject);
                    context.Response.Write(html);
                }
                else if (type.Equals("GetStar"))
                {
                    BLL.Props.Props BLLRProps = new Jy123.BLL.Props.Props();
                    List<Model.Club_PropsRecord> listRecord = BLLRProps.GetPropsRecord(user.ID, club.FJType.Value, "1");
                    Dictionary<int, Model.Public_Props> dicProps = new Dictionary<int, Jy123.Model.Public_Props>();

                    List<Model.Public_Props> listProps = BLLRProps.GetProps(club.FJType.Value, "1");
                    foreach (Model.Public_Props cProps in listProps)
                    {
                        dicProps.Add(cProps.ID, cProps);
                    }

                    foreach (Model.Club_PropsRecord record in listRecord)
                    {
                        string strPart = context.Cache["xmlvenuespart"].ToString();
                        Model.Public_Props cProps = dicProps[record.FProps.Value];
                        strPart = strPart.Replace("$PartId$", cProps.ID.ToString());
                        strPart = strPart.Replace("$DisplayName$", cProps.PName);
                        strPart = strPart.Replace("$DisplayOrder$", cProps.DisplayOrder.ToString());
                        string isShow = "0";
                        if (record.IsUsed)
                        {
                            isShow = "1";
                        }
                        strPart = strPart.Replace("$IsShow$", isShow);
                        strPart = strPart.Replace("$ImageUrl$", cProps.Model);
                        strPart = strPart.Replace("$PosX$", cProps.PosX.ToString());
                        strPart = strPart.Replace("$PosY$", cProps.PosY.ToString());
                        strPart = strPart.Replace("$BtnImageUrl$", cProps.SmallModel);
                        sbString.Append(strPart);
                    }
                    string backImg = "/Cache/Module/Flash/CourtImages/f1_term_background.png";
                    //需要改进，暂时写死
                    if (club.FJType == 7)
                    {
                        backImg = "/Cache/Module/Flash/CourtImages/ReadyForFb.jpg";
                    }
                    strResult = strResult.Replace("$BackImg$", backImg);
                    strResult = strResult.Replace("$Part$", sbString.ToString());
                    strResult = strResult.Replace("$Part$", string.Empty);
                    strResult = strResult.Replace("CourtBtn_background.jpg", "CourtBtn_backgroundclub.jpg");
                    byte[] byBuffer = System.Text.Encoding.Default.GetBytes(strResult);
                    context.Response.BinaryWrite(byBuffer);
                }
                else
                {
                    BLL.Club_FittingsRecord BLLRecord = new Jy123.BLL.Club_FittingsRecord();
                    BLL.Club_Cars BLLCars = new Jy123.BLL.Club_Cars();
                    BLL.Public_Cars BLLPCars = new Jy123.BLL.Public_Cars();
                    BLL.Public_Fittings BLLFit = new Jy123.BLL.Public_Fittings();
                    List<Model.Public_Cars> listAllCars = BLLPCars.GetModelList(string.Empty);
                    Dictionary<int, Model.Public_Cars> dicCars = new Dictionary<int, Jy123.Model.Public_Cars>();
                    foreach (Model.Public_Cars car in listAllCars)
                    {
                        dicCars.Add(car.ID, car);
                    }
                    List<Model.Public_Fittings> listAllF = BLLFit.GetModelList(string.Empty);
                    Dictionary<int, Model.Public_Fittings> dicFits = new Dictionary<int, Jy123.Model.Public_Fittings>();
                    foreach (Model.Public_Fittings fit in listAllF)
                    {
                        dicFits.Add(fit.ID, fit);
                    }
                    List<Model.Club_Cars> listCars = BLLCars.GetModelList(string.Format("FClub = {0} AND IsUsed =1", club.ID.ToString()));
                    List<Model.Club_FittingsRecord> listRecord = BLLRecord.GetModelList("FClub = " + club.ID.ToString());
                    if (listCars.Count > 0)
                    {
                        Model.Club_Cars cCar = listCars[0];
                        Model.Public_Cars pCar = dicCars[cCar.FClubCarsType.Value];
                        strResult = strResult.Replace("$BackImg$", pCar.BigModel);
                        foreach (Model.Club_FittingsRecord record in listRecord)
                        {
                            string strPart = context.Cache["xmlvenuespart"].ToString();
                            Model.Public_Fittings cFit = dicFits[record.FFittings.Value];
                            strPart = strPart.Replace("$PartId$", cFit.ID.ToString());
                            strPart = strPart.Replace("$DisplayName$", cFit.FName);
                            strPart = strPart.Replace("$DisplayOrder$", cFit.DisplayOrder.Value.ToString());
                            string isShow = "0";
                            if (record.IsUsed)
                            {
                                isShow = "1";
                            }
                            strPart = strPart.Replace("$IsShow$", isShow);
                            strPart = strPart.Replace("$ImageUrl$", cFit.Model);
                            strPart = strPart.Replace("$PosX$", cFit.PosX.ToString());
                            strPart = strPart.Replace("$PosY$", cFit.PosY.ToString());
                            strPart = strPart.Replace("$BtnImageUrl$", cFit.SmallModel);
                            sbString.Append(strPart);
                        }
                        strResult = strResult.Replace("$Part$", sbString.ToString());
                        strResult = strResult.Replace("$Part$", string.Empty);
                        strResult = strResult.Replace("CourtBtn_background.jpg", "CourtBtn_backgroundclub.jpg");
                        byte[] byBuffer = System.Text.Encoding.Default.GetBytes(strResult);
                        context.Response.BinaryWrite(byBuffer);
                    }
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}
